const Koa = require('koa')
const pug = require('pug')
const views = require('koa-views')
const mongoose = require('mongoose')
const { join } = require('path')

const R = require('ramda')
const MIDDLEWARES = ['common', 'router', 'parcel']

const { connect, initSchemas, initAdmin } = require('./database/init') 

const app = new Koa()

// 调用中间键
const useMiddlewares = app => { 
  R.map(
    R.compose(
      R.forEachObjIndexed(
        initWith  => initWith(app) 
      ),
      require,
      name => join(__dirname, `./middlewares/${name}`)
    )
  )(MIDDLEWARES)
}

;(async () => {
  await connect()
  initSchemas()
  await initAdmin()
  // require('./tasks/movie')
  // require('./tasks/api')
  // require('./tasks/trailer')
  // require('./tasks/qiniu')
  await useMiddlewares(app)

  app.listen(8080, () => {
    console.log('http://localhost:8080')
  })
})()

